<template>
  <div class="app-container">
    <el-card shadow="never">
      <el-form :inline="true" :model="listQuery" size="small" label-width="100px">
        <el-form-item label="表名称：">
          <el-input v-model="listQuery.tableName" class="input-width" placeholder="请输入表名称" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button size="small" type="primary" @click="getDataList()">
            搜索
          </el-button>
          <el-button size="small" @click="handleResetSearch()">
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <div class="table-container">
      <el-table :data="list" border v-loading="dataListLoading" style="width: 100%;">
        <el-table-column prop="tableName" header-align="center" align="center" label="表名称">
        </el-table-column>
        <el-table-column prop="engine" header-align="center" align="center" label="数据库类型">
        </el-table-column>
        <el-table-column prop="tableComment" header-align="center" align="center" label="表备注">
        </el-table-column>
        <el-table-column prop="createTime" header-align="center" align="center" label="创建时间">
        </el-table-column>
        <el-table-column fixed="right" header-align="center" align="center" width="150" label="操作">
          <template slot-scope="scope">
            <el-button type="primary" size="small" @click="genCode(scope.row.tableName)">代码生成</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-pagination background class="table-pagination" @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle" :current-page="listQuery.pageIndex" :page-size="listQuery.pageSize"
      :total="total" :page-sizes="[10, 20, 50, 100]" layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
  </div>
</template>

<script>
  import axios from 'axios';
  import { getToken } from '@/utils/auth';
  import { saveAs } from 'file-saver';
  import { fetchList } from '@/api/gen/gen';
  const baseURL = process.env.VUE_APP_BASE_API;
  const defaultListQuery = {
    pageIndex: 1,
    pageSize: 10,
    tableName: ''
  };
  export default {
    data() {
      return {
        listQuery: Object.assign({}, defaultListQuery),
        list: [],
        total: 0,
        dataListLoading: false,
      }
    },
    created() {
      this.getDataList()
    },
    methods: {
      // 重置查询表单
      handleResetSearch() {
        this.listQuery = Object.assign({}, defaultListQuery);
        this.getDataList();
      },
      // 获取数据列表
      getDataList() {
        this.dataListLoading = true
        fetchList(this.listQuery).then(response => {
          this.list = response.data.records
          this.total = response.data.total
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle(val) {
        this.listQuery.pageSize = val
        this.listQuery.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle(val) {
        this.listQuery.pageIndex = val
        this.getDataList()
      },
      // 立即执行
      genCode(tableName) {
        this.$confirm(`确定生成代码?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          axios({
            method: 'get',
            url: baseURL + "/sys/gen/code?tables=" + tableName,
            responseType: 'blob',
            headers: { 'SalonAdminToken': getToken() }
          }).then((res) => {
            if (res.data.type !== 'application/json') {
              const blob = new Blob([res.data], { type: 'application/zip' })
              saveAs(blob, tableName + ".zip")
            } else {
              this.printErrMsg(res.data);
            }
          })
        }).catch(() => {})
      },
    }
  }
</script>
